<?php
include_once("Classes/MediaItemTemplateFactory.php");
include_once("Classes/MediaDAO.php");

$mediaItemDAO = new MediaDAO();
$mediaItem = new MediaBean();

//if this action is recieving a post, then process it
if(isset($_POST['id']) && $_POST['id']!="") {
	//load the posted information into the media bean
	$mediaItem->setId($_POST['id']);
	$mediaItem->setTitle($_POST['title']);
	$mediaItem->setUrl($_POST['url']);
	$mediaItem->setSpeaker($_POST['speaker']);
	$mediaItem->setPostedDate($_POST['date']." ".$_POST['time']);
	$mediaItem->setCollId($_POST['coll_id']);

	//depending on the request send the media bean to the appropriate processing method 
	if(isset($_GET['delete'])) {
		$mediaItemDAO->executeChangeById("DELETE",$mediaItem);
	}
	elseif(isset($_GET['new'])) {
		$mediaItemDAO->executeChangeById("INSERT",$mediaItem);
	}
	else {
		$mediaItemDAO->executeChangeById("UPDATE",$mediaItem);
	}
	
	header("Location: index.php?action=media&id=" . $_GET['collId']);
}

//map the URL paramter to the dataBean, load it, and show the form with the data
if(isset($_GET['id']) && $_GET['id'] != "") {
	$lookupBean = new MediaBean();
	
	$lookupBean->setId($_GET['id']);
	$mediaItem->setId($lookupBean->getId());
	
	$mediaItemArray = $mediaItemDAO->getRecordSetById("SELECT",$mediaItem);
	$dataBean = $mediaItemArray[0];	
	
	$mediaItemTemplate = MediaItemTemplateFactory::createMediaItemTemplate("audio");
	$mediaItemTemplate->printTemplate($dataBean);
}
else {
	$blankBean = new MediaBean();
	$blankBean->setId("NEW");
	$blankBean->setCollId($_GET['collId']);
		
	$mediaItemTemplate = MediaItemTemplateFactory::createMediaItemTemplate("audionew");
	$mediaItemTemplate->printTemplate($blankBean);
}
?>